
#include <QApplication>
#include <QDebug>
#include <QGuiApplication>
#include <QPushButton>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include <QQmlContext>
#include <QQmlEngine>
#include <QWidget>
#include <QWindow>

#include "MyForm.h"
#include "MyLoadDialogCmd.h"

/**
 * @brief qMain
 * @param argc
 * @param argv
 * @return
 */
int main(int argc, char* argv[]) {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    // if you want to use qwidget, must use QApplication
    QApplication app(argc, argv);

    // QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    const QUrl            url(QStringLiteral("qrc:/main.qml"));
    engine.load(url);
    if (engine.rootObjects().isEmpty()) {
        qDebug() << "Load qrc:/main.qml error!";
        return -1;
    }

    QObject* qmlObj    = engine.rootObjects().first();   // first QML object
    QWindow* qmlWindow = qobject_cast<QWindow*>(qmlObj); // to QWindow

    MyLoadDialogCmd cmd; // command

    // pass command to engine
    engine.rootContext()->setContextProperty("g_MyCommand", &cmd);

    QWidget* myForm = cmd.initialDialog(); // a widget

    if (myForm) {
        WId parent_HWND = qmlWindow->winId();
        myForm->winId(); // create a win id
        myForm->setProperty("_q_embedded_native_parent_handle", QVariant(parent_HWND));
        myForm->windowHandle()->setParent(qmlWindow);
        myForm->show();
    }

    int c = app.exec();

    // release
    delete myForm;
    myForm = nullptr;
    return c;
}
